// var r;
// var AL_GAME_HUA_AddPrefab = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_AddPrefab");
// var AL_GAME_HUA_Common = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Common");
// var AL_GAME_HUA_Define = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Define");
// var AL_GAME_HUA_Instance = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Instance");
// var AL_GAME_HUA_PopupSelf = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PopupSelf");
// var AL_GAME_HUA_EventConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_EventConfig");
// var AL_GAME_HUA_GoodsConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_GoodsConfig");
// var AL_GAME_HUA_OnlineGiftConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_OnlineGiftConfig");
// var g = cc._decorator;
// var P = g.ccclass;
// var m = g.property;
// var h =
//     (g.menu,
//     (function (e) {
//         function o() {
//             var o = (null !== e && e.apply(this, arguments)) || this;
//             o.node_content = null;
//             o.speedup_Button = null;
//             o.recieve_Button = null;
//             o.label_time = null;
//             o.progress_bar = null;
//             o.light = null;
//             o.reward_Nodes = [];
//             return o;
//         }
//         __extends(o, e);
//         o.prototype.start = function () {
//             this.reward_Nodes = this.node_content.children;
//             this.shuaxin_anniu();
//             this.shuaxin_jianli_neirong();
//             AL_GAME_HUA_Instance.Instance.xianshi_dingbu_tiao({
//                 target: this.node,
//                 showAll: !0
//             });
//             if (AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_shifou_jiesuo()) {
//                 this.progress_bar.progress = 0;
//                 this.label_time.string = AL_GAME_HUA_Common.Common.geshihua_shijian(0, !0);
//             }
//             cc.tween(this.light)
//                 .by(5, {
//                     angle: 360
//                 })
//                 .repeatForever()
//                 .start();
//         };
//         o.prototype.shuaxin_anniu = function () {
//             this.speedup_Button.active =
//                 !AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_shifou_jiesuo();
//             this.recieve_Button.active =
//                 AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_shifou_jiesuo();
//         };
//         o.prototype.shuaxin_jianli_neirong = function () {
//             var e = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_rule();
//             var o = [
//                 AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt,
//                 AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush,
//                 AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze,
//                 AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset,
//                 AL_GAME_HUA_GoodsConfig.GoodsId.gold
//             ];
//             var t = function (t) {
//                 var r = n.reward_Nodes[t].getChildByName("tanchu_icon_tip").getComponent(cc.Sprite);
//                 n.reward_Nodes[t].getChildByName("count").getComponent(cc.Label).string = "x" + e[t];
//                 AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance()
//                     .jiazai_tubiao_icon(o[t])
//                     .then(function (e) {
//                         if (cc.isValid(r)) {
//                             r.spriteFrame = e;
//                             r.addAutoReleaseAssets(e);
//                             AL_GAME_HUA_Common.Common.uniform_scale(r, 180, 180);
//                         }
//                     });
//             };
//             var n = this;
//             for (var r = 0; r < e.length; r++) {
//                 t(r);
//             }
//         };
//         o.prototype.update = function () {
//             if (!AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_shifou_jiesuo()) {
//                 this.label_time.string = AL_GAME_HUA_Common.Common.geshihua_shijian(
//                     1e3 * AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_shengyu_shijian(),
//                     !0
//                 );
//                 var e =
//                     AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().giftTime /
//                     AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_xuyao_shijian();
//                 this.progress_bar.progress = 1 - e;
//             }
//         };
//         o.prototype.onEnable = function () {
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
//                 node: this.node,
//                 hide: !0
//             });
//         };
//         o.prototype.onDisable = function () {
//             AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, {
//                 node: this.node,
//                 hide: !1
//             });
//         };
//         o.prototype.handle_button_callback = function (e, o) {
//             var t = this;
//             switch (o) {
//                 case "close":
//                     this.close();
//                     break;
//                 case "speedup":
//                     AL_GAME_HUA_Common.Common.showVideo({
//                         success: function (e) {
//                             var o = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_xuyao_shijian();
//                             AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().shezhi_zaixian_jiangli_jiesuo(!0);
//                             t.shuaxin_anniu();
//                             cc.tween(t.progress_bar)
//                                 .to(
//                                     0.5,
//                                     {
//                                         progress: 0
//                                     },
//                                     {
//                                         progress: function (e, n, r, i) {
//                                             var s = e + (n - e) * i;
//                                             t.progress_bar.progress = s;
//                                             t.label_time.string = AL_GAME_HUA_Common.Common.geshihua_shijian(o * s * 1e3, !0);
//                                             return s;
//                                         }
//                                     }
//                                 )
//                                 .start();
//                             AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportTransform({
//                                 source: AL_GAME_HUA_EventConfig.AssetsSource.onlineGift,
//                                 action:
//                                     2 == e.type
//                                         ? AL_GAME_HUA_EventConfig.AssetsAction.video_get
//                                         : AL_GAME_HUA_EventConfig.AssetsAction.share_get,
//                                 goodsList: [
//                                     {
//                                         goods: AL_GAME_HUA_GoodsConfig.GoodsId.speedUp,
//                                         num: 1
//                                     }
//                                 ]
//                             });
//                         }
//                     });
//                     break;
//                 case "recieve":
//                     AL_GAME_HUA_Common.Common.showVideo({
//                         success: function (e) {
//                             var o = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_rule();
//                             AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().chuli_lingqu();
//                             AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_jieshou_mianban({
//                                 rewards: [
//                                     {
//                                         goodsId: AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt,
//                                         num: o[0]
//                                     },
//                                     {
//                                         goodsId: AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush,
//                                         num: o[1]
//                                     },
//                                     {
//                                         goodsId: AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze,
//                                         num: o[2]
//                                     },
//                                     {
//                                         goodsId: AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset,
//                                         num: o[3]
//                                     },
//                                     {
//                                         goodsId: AL_GAME_HUA_GoodsConfig.GoodsId.gold,
//                                         num: o[4]
//                                     }
//                                 ]
//                             });
//                             AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
//                                 source: AL_GAME_HUA_EventConfig.AssetsSource.onlineGift,
//                                 action:
//                                     2 == e.type
//                                         ? AL_GAME_HUA_EventConfig.AssetsAction.video_get
//                                         : AL_GAME_HUA_EventConfig.AssetsAction.share_get,
//                                 goodsList: [
//                                     {
//                                         goods: AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt,
//                                         num: o[0]
//                                     },
//                                     {
//                                         goods: AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush,
//                                         num: o[1]
//                                     },
//                                     {
//                                         goods: AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze,
//                                         num: o[2]
//                                     },
//                                     {
//                                         goods: AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset,
//                                         num: o[3]
//                                     },
//                                     {
//                                         goods: AL_GAME_HUA_GoodsConfig.GoodsId.gold,
//                                         num: o[4]
//                                     }
//                                 ]
//                             });
//                             t.shuaxin_jianli_neirong();
//                             t.shuaxin_anniu();
//                             t.close();
//                         }
//                     });
//             }
//         };
//         __decorate([m(cc.Node)], o.prototype, "node_content", void 0);
//         __decorate([m(cc.Node)], o.prototype, "speedup_Button", void 0);
//         __decorate([m(cc.Node)], o.prototype, "recieve_Button", void 0);
//         __decorate([m(cc.Label)], o.prototype, "label_time", void 0);
//         __decorate([m(cc.ProgressBar)], o.prototype, "progress_bar", void 0);
//         __decorate([m(cc.Node)], o.prototype, "light", void 0);
//         return __decorate([P], o);
//     })(AL_GAME_HUA_PopupSelf.default));
// exports.default = h;

const AL_GAME_HUA_AddPrefab = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_AddPrefab");
const AL_GAME_HUA_Common = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Common");
const AL_GAME_HUA_Define = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Define");
const AL_GAME_HUA_Instance = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_Instance");
const AL_GAME_HUA_PopupSelf = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_PopupSelf");
const AL_GAME_HUA_EventConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_EventConfig");
const AL_GAME_HUA_GoodsConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_GoodsConfig");
const AL_GAME_HUA_OnlineGiftConfig = require("../../AL_GAME_HUA/scripts/AL_GAME_HUA_OnlineGiftConfig");

const { ccclass, property, menu } = cc._decorator;

/**
 * 处理在线礼物的弹窗
 */
@ccclass
class OnlineGiftPopup extends AL_GAME_HUA_PopupSelf.default {
    /** @type {cc.Node} 礼物内容节点 */
    @property(cc.Node)
    node_content = null;

    /** @type {cc.Node} 加速按钮 */
    @property(cc.Node)
    speedup_Button = null;

    /** @type {cc.Node} 接收按钮 */
    @property(cc.Node)
    recieve_Button = null;

    /** @type {cc.Label} 时间标签 */
    @property(cc.Label)
    label_time = null;

    /** @type {cc.ProgressBar} 进度条 */
    @property(cc.ProgressBar)
    progress_bar = null;

    /** @type {cc.Node} 光标 */
    @property(cc.Node)
    light = null;

    /** @type {Array.<cc.Node>} 奖励节点数组 */
    reward_Nodes = [];

    /**
     * 开始时的初始化
     */
    start() {
        this.reward_Nodes = this.node_content.children;
        this.shuaxin_anniu();
        this.shuaxin_jianli_neirong();
        AL_GAME_HUA_Instance.Instance.xianshi_dingbu_tiao({ target: this.node, showAll: true });

        if (AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_shifou_jiesuo()) {
            this.progress_bar.progress = 0;
            this.label_time.string = AL_GAME_HUA_Common.Common.geshihua_shijian(0, true);
        }

        cc.tween(this.light)
            .by(5, { angle: 360 })
            .repeatForever()
            .start();
    }

    /**
     * 刷新按钮状态
     */
    shuaxin_anniu() {
        const isGiftUnlocked = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_shifou_jiesuo();
        this.speedup_Button.active = !isGiftUnlocked;
        this.recieve_Button.active = isGiftUnlocked;
    }

    /**
     * 刷新奖励内容
     */
    shuaxin_jianli_neirong() {
        const giftRules = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_rule();
        const goodsIds = [
            AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt,
            AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush,
            AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze,
            AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset,
            AL_GAME_HUA_GoodsConfig.GoodsId.gold
        ];

        const updateReward = (index) => {
            const rewardNode = this.reward_Nodes[index];
            const icon = rewardNode.getChildByName("tanchu_icon_tip").getComponent(cc.Sprite);
            const countLabel = rewardNode.getChildByName("count").getComponent(cc.Label);
            countLabel.string = `x${giftRules[index]}`;

            AL_GAME_HUA_GoodsConfig.GoodsConfig.getInstance()
                .jiazai_tubiao_icon(goodsIds[index])
                .then((spriteFrame) => {
                    if (cc.isValid(icon)) {
                        icon.spriteFrame = spriteFrame;
                        icon.addAutoReleaseAssets(spriteFrame);
                        AL_GAME_HUA_Common.Common.uniform_scale(icon, 180, 180);
                    }
                });
        };

        for (let i = 0; i < giftRules.length; i++) {
            updateReward(i);
        }
    }

    /**
     * 每帧更新
     */
    update() {
        if (!AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_shifou_jiesuo()) {
            const remainingTime = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_shengyu_shijian() * 1000;
            this.label_time.string = AL_GAME_HUA_Common.Common.geshihua_shijian(remainingTime, true);

            const progress = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().giftTime /
                AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_xuyao_shijian();
            this.progress_bar.progress = 1 - progress;
        }
    }

    /**
     * 节点启用时触发
     */
    onEnable() {
        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, { node: this.node, hide: true });
    }

    /**
     * 节点禁用时触发
     */
    onDisable() {
        AL_APP_SDK_MANAGER.emit(AL_GAME_HUA_Define.GameEvent.ON_GAME_CLUB_HIDE, { node: this.node, hide: false });
    }

    /**
     * 按钮点击回调处理函数
     * @param {cc.Event.EventCustom} event - 事件对象
     * @param {string} action - 按钮动作类型
     */
    handle_button_callback(event, action) {
        switch (action) {
            case "close":
                this.close();
                break;
            case "speedup":
                this.handle_Speedup();
                break;
            case "recieve":
                this.handle_Receive();
                break;
        }
    }

    /**
     * 处理加速按钮点击
     */
    handle_Speedup() {
        AL_GAME_HUA_Common.Common.showVideo({
            success: (result) => {
                const neededTime = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_xuyao_shijian();
                AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().shezhi_zaixian_jiangli_jiesuo(true);
                this.shuaxin_anniu();

                cc.tween(this.progress_bar)
                    .to(0.5, { progress: 0 }, {
                        progress: (start, end, ratio, t) => {
                            const current_progress = start + (end - start) * t;
                            this.progress_bar.progress = current_progress;
                            this.label_time.string = AL_GAME_HUA_Common.Common.geshihua_shijian(neededTime * current_progress * 1000, true);
                            return current_progress;
                        }
                    })
                    .start();

                AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportTransform({
                    source: AL_GAME_HUA_EventConfig.AssetsSource.onlineGift,
                    action: result.type === 2 
                        ? AL_GAME_HUA_EventConfig.AssetsAction.video_get 
                        : AL_GAME_HUA_EventConfig.AssetsAction.share_get,
                    goodsList: [{ goods: AL_GAME_HUA_GoodsConfig.GoodsId.speedUp, num: 1 }]
                });
            }
        });
    }

    /**
     * 处理接收按钮点击
     */
    handle_Receive() {
        AL_GAME_HUA_Common.Common.showVideo({
            success: (result) => {
                const giftRules = AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().huoqu_zaixian_jiangli_rule();
                AL_GAME_HUA_OnlineGiftConfig.OnlineGiftConfig.getInstance().chuli_lingqu();

                AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_jieshou_mianban({
                    rewards: giftRules.map((num, index) => {
                        return {
                            goodsId: [
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt,
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush,
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze,
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset,
                                AL_GAME_HUA_GoodsConfig.GoodsId.gold
                            ][index],
                            num
                        };
                    })
                });

                AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportOutput({
                    source: AL_GAME_HUA_EventConfig.AssetsSource.onlineGift,
                    action: result.type === 2 
                        ? AL_GAME_HUA_EventConfig.AssetsAction.video_get 
                        : AL_GAME_HUA_EventConfig.AssetsAction.share_get,
                    goodsList: giftRules.map((num, index) => {
                        return {
                            goods: [
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_prompt,
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_brush,
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_freeze,
                                AL_GAME_HUA_GoodsConfig.GoodsId.prop_reset,
                                AL_GAME_HUA_GoodsConfig.GoodsId.gold
                            ][index],
                            num
                        };
                    })
                });

                this.shuaxin_jianli_neirong();
                this.shuaxin_anniu();
                this.close();
            }
        });
    }
}

exports.default = OnlineGiftPopup;
